<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <div>
        <el-button type="primary" @click="showAddUserDialog">添加分类</el-button>
      </div>
      <!-- 表格 -->

      <tree-table
        :data="cateList"
        border
        :selection-type="false"
        :show-row-hover="false"
        index-text="#"
        :expand-type="false"
        show-index
        :columns="columns"
      >
        <template slot="isok" slot-scope="scope">
          <!-- {{scope.row.cat_deleted}} -->
          <i style="color:green" v-if="scope.row.cat_deleted===false" class="el-icon-success"></i>

          <i style="color:red" v-else class="el-icon-error"></i>
        </template>
        <!-- 排序的模板 -->
        <template slot="order" slot-scope>
          <div>123</div>
        </template>
        <!-- 操作的模板 -->
        <template slot="options" slot-scope="">
          <el-button size="mini" type="primary" icon="el-icon-delete">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
        <!-- 第几页 -->
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 4, 6, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 对话框 -->
    <!-- 别绑错了 -->
    <!-- 忘记写el -form了 -->
    <el-dialog title="添加用户" width="30%" :visible.sync="addUserDialogVisible">
      <el-form :model="addCateForm" :rules="addCateRules" ref="addCateForm" label-width="100px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader
            v-model="cascaderVulue"
            :options="parentCateList"
            :props="cascaderProps"
            clearable
            @change="cascaderHandleChange"
          
          ></el-cascader>
          
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addUserDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      addUserDialogVisible: false,
      cateList: [],
      parentCateList: [],
      cascaderVulue: [],
      cascaderProps: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        checkStrictly:true
      },
      total: 0,
      addCateForm: {
        cat_pid: 0,
        cat_name: "",
        cat_lever: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },

      columns: [
        { label: "分类名称", prop: "cat_name" },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "options"
        }
      ]
    };
  },
  created() {
    this.getGoodsList();
  },

  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200)
        return this.$message.error("保存数据失败失败");

      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    async getRarentCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status !== 200) return this.$message.error("上传失败");
      this.parentCateList = res.data;
    },

    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    showAddUserDialog() {
      this.addUserDialogVisible = true;
      this.getRarentCateList;
    },
    //选择器发生变化
    cascaderHandleChange(){
      // console.log(this.cascaderValue);
       if(this.cascaderVulue.length>0) {
         this.addCateForm.cat_lever=this.cascaderVulue.length
         this.addCateForm.cat_pid=this.cascaderVulue[this.cascaderVulue.length]
       }else{
        //  this.$refs.addCateRef.resetFields()
         this.addCateForm.cat_lever=0
        //  this.cascaderVulue=[]
         this.addCateForm.cat_pid=0
       }
    },
    //添加分类
    addCate(){
      // console.log(this.addCateForm);
       this.$refs.addCateRef.validate(async flag=>{
           if(flag) return this.$message.error('请完善表单') 
           const{data:res}=await this.$http.post('categories',this.addCateForm)
           if(res.meta.status !==201) return this.$message.error('添加失败')

           this.$message.success('添加成功')
           this.addCateDialogVisible=false
           this.parentCateList=res.data
           this.getGoodsList()
       })
    },
    addCateClose(){
          this.$refs.addCateRef.resetFields()
        this.cascaderVulue=[]
         this.addCateForm.cat_lever=0
         this.addCateForm.cat_pid=0
    }

  }
};
</script>
<style scoped lang="less">
.el-cascader{
  width:100%
}
</style>